package com.LittleHan.crm.settings.controller;



import com.LittleHan.crm.exception.LoginException;
import com.LittleHan.crm.settings.entity.User;
import com.LittleHan.crm.settings.service.UserService;
import com.LittleHan.crm.utils.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Controller(value ="userController" )
@RequestMapping(value = "/settings/user")
public class UserController {
    @Autowired
    @Qualifier(value = "userServiceImpl")
    UserService userService;
    @RequestMapping(value="/login.do")
    public String login(){
        return "redirect:/login.jsp";
    }
/*
    十天免登录
 */
    @RequestMapping(value="/cookie.do")
    public String cookieToIndex(HttpServletRequest request,HttpServletResponse response) throws LoginException {
        Cookie cookie[]=request.getCookies();
        String userName=null;
        String userPsw=null;
        if(cookie!=null){
            for(Cookie c:cookie){
                String name=c.getName();
                if("name".equals(name)){
                    userName=c.getValue();
                }
                if("psw".equals(name)){
                    userPsw=c.getValue();
                }
            }
        }

        //System.out.println(userName+",,,,"+userPsw);
        if(userName!=null && userPsw!=null && !"".equals(userName) && !"".equals(userPsw)){
            String ip=request.getRemoteAddr();
            User user=userService.login(userName,userPsw,ip);
            request.getSession().setAttribute("user",user);
            return "/workbench/toIndex.do";
        }
        //return "redirect:/login.jsp";
        return "/settings/user/login.do";
    }


    @RequestMapping(value="/toLogin.do",method = RequestMethod.POST,produces = "text/plain;charset=utf-8")
    @ResponseBody
    public String fun(HttpServletRequest request, HttpServletResponse response, String userName, String password,String tenDay) throws LoginException {
        String ip=request.getRemoteAddr();//获取登录ip地址
        String pwd= MD5Util.getMD5(password);//将密码采用md5方式加密
        //调用service层的login方法，如果为空(也就说明密码或者用户名错误)，会抛出异常交给异常处理类统一处理
        User user=userService.login(userName,pwd,ip);
        //以下代码能运行说明上条代码没有抛出异常
        HttpSession session=request.getSession();//获取会话作用域。
        session.setAttribute("user",user);//将user放入会话作用域；
        if("yes".equals(tenDay)){
            Cookie cookieName=new Cookie("name",userName);//cookie只能存入字符串
            Cookie cookiePsw=new Cookie("psw",pwd);
            cookieName.setPath("/");
            cookiePsw.setPath("/");
            cookieName.setMaxAge(60*60*24*10);//设置cookie存活时间，秒为基本单位，为10天
            cookiePsw.setMaxAge(60*60*24*10);
            response.addCookie(cookieName);//写到响应包，交给浏览器
            response.addCookie(cookiePsw);
        }


        return "{\"success\":\"true\"}";//打给ajax响应
    }

}
